package com.yunxun.coinmessage.dao;

import com.yunxun.coinmessage.entity.Subscribe;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import java.util.List;
@Mapper
@Component
public interface  SubscribeDao {
    @Insert("insert into subscribe(model_id,uid,type_id,create_time)values(#{model_id},#{uid},#{type_id},#{create_time})")
    Integer addSubscribe(Subscribe subscribe);
    //改变订阅的状态
    @Update("update subscribe set status=1 where uid=#{uid} and model_id=#{model_id} and type_id=#{type_id}")
    Integer updateSubscribeStatus(Subscribe subscribe);

    @Select("select * from subscribe where status=1  order by uid desc ,sub_id desc limit #{start},#{limit}")
    List<Subscribe> querySubscribeByPage(@Param("start") int start,@Param("limit") int limit);
    @Select("select count(*) from subscribe where status !=0")
    Integer queryTotal();
    @Select("select * from subscribe where uid=#{uid} and status=1")
    List<Subscribe> querySubscribeByUid(int uid);
    //查询是否有重复的订阅
    @Select("select count(*) from subscribe where uid=#{uid} and type_id=#{type_id} and model_id=#{model_id} and status=1")
    int queryBySubscribe(Subscribe subscribe);

    /**
     * 查询数据是否存在于数据库
     * @param subscribe
     * @return
     */
    @Select("select count(*) from subscribe where uid=#{uid} and type_id=#{type_id} and model_id=#{model_id} and status=0")
    int queryExistDatabase(Subscribe subscribe);

    /**
     * 根据type_id和model_id查询订阅了该栏目模型的用户id
     * 考虑到订阅了该资讯的用户数据量过大用了分页查询,一次100.
     * @param type_id 栏目id
     * @param model_id 模型id
     * @return
     */
    @Select("select uid from subscribe where type_id=#{type_id} and model_id=#{model_id} and status=1 limit #{start},#{limit}")
    List<Integer> queryUidByCcntent(@Param("type_id") int type_id,@Param("model_id") int model_id,@Param("start") int start,@Param("limit")int limit);

    /**
     * 根据type_id和model_id查询总订阅数
     * @param type_id 栏目id
     * @param model_id 模型id
     */
    @Select("select count(*) from subscribe where  type_id=#{type_id} and  model_id=#{model_id} and status=1")
    int queryCountByContent(@Param("type_id") int type_id,@Param("model_id") int model_id);

    /**
     * 查询订阅了快讯的所有总用户数量
     * @return
     */
    @Select("select count(*) from subscribe where type_id=1 and status=1")
    int queryQuickNewsUserTotal();

    @Select("select uid from subscribe where type_id=1 and status=1 limit #{start},#{limit}")
    List<Integer> queryQuickNewsUser(@Param("start")int start,@Param("limit")int limit);

    /**
     * 判断该用户是否订阅了快讯，不为0证明该用户订阅了快讯
     * @param uid
     * @return
     */
    @Select("select count(*) from subscribe where type_id=1 and status=1 and uid=#{uid} ")
    public int judgeSubQuickNews(int uid);

    /**
     * 查询订阅了交易所的总用户数
     * @return
     */
    @Select("select count(*) from subscribe where type_id=3 and status=1")
    public int queryExchangeTotal();

    /**
     * 查询订阅了交易所的所有用户
     * @param start
     * @param limit
     * @return
     */
    @Select("select uid from subscribe where type_id=3 and status=1 limit #{start},#{limit}")
    public List<Integer> queryExchangeUid(@Param("start") int start,@Param("limit") int limit);

    /**
     * 判断某个用户是否订阅了该币种，不为0即是订阅了
     * @param uid
     * @param coin_id
     * @return
     */
    @Select("select count(*) from subscribe where uid=#{uid} and type_id=2 and status=1 and model_id=#{coin_id}")
    public int judgeSubCoin(@Param("uid") int uid,@Param("coin_id") int coin_id);

   @Select("select count(*) from subscribe where uid=#{uid} and type_id=3 and status=1")
    public int judgeSubExchange(int uid);

    public List<Subscribe> querySubscribeByUidList(List<Integer> uidList);
}
